package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import orm.ChildrenBehaviorPurpose;
import orm.CollectivityMonthWeekAim;
import orm.CollectivityWeekTimeBucket;
import orm.IndividualMonthWeekAim;
import orm.TopicTransferModel;
import util.DBHelper;

public class IMonthWeekAimDAO {
	
	public void insertIMonthWeekAim(Statement statement, 
			ArrayList<IndividualMonthWeekAim> imwaArr, String iWeekArrangeID) throws SQLException {
		IndividualMonthWeekAim imwa = null;
		for (int i = 0; i < imwaArr.size(); i++) {
			imwa = imwaArr.get(i);
			statement.execute("insert into individualmonthweekaim values('" + iWeekArrangeID + "'," +
					imwa.getWeekID() + ", " + imwa.getWeekAimID() + ", '" + imwa.getTPID() +
					"', '" + imwa.getCorePurpose() + "')");
		}
	}
	
	public void updateIMonthWeekAim(Statement statement,
			ArrayList<IndividualMonthWeekAim> imwaArr, String iWeekArrangeID) throws SQLException {
		IndividualMonthWeekAim imwa = null;
		for (int i = 0; i < imwaArr.size(); i++) {
			imwa = imwaArr.get(i);
			StringBuffer sqlStr = new StringBuffer();
			sqlStr.append("update individualmonthweekaim set TPID = '");
			sqlStr.append(imwa.getTPID() + "', CorePurpose = '" + imwa.getCorePurpose());
			sqlStr.append("' where IWeekArrangeID = '");
			sqlStr.append(imwa.getIWeekArrangeID() + "' and WeekID = " + imwa.getWeekID() + " and WeekAimID = " + imwa.getWeekAimID() + "");
			
			statement.execute(sqlStr.toString());
		}
	}
	
	public void deleteIMonthWeekAim(Statement statement, String iWeekArrangeID) throws SQLException {
		statement.execute("delete from individualmonthweekaim where IWeekArrangeID='" + iWeekArrangeID + "'");
	}
	
	public ArrayList<IndividualMonthWeekAim> getimwaByCWAID(String iWeekArrangeID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		ArrayList<IndividualMonthWeekAim> al = new ArrayList<IndividualMonthWeekAim>();
		IndividualMonthWeekAim imwa = null;
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from individualmonthweekaim where IWeekArrangeID='" + iWeekArrangeID + "'");
		
		while (rs.next()) {
			imwa = new IndividualMonthWeekAim();
			int i = 1;
			imwa.setIWeekArrangeID(rs.getString(i++));
			imwa.setWeekID(rs.getInt(i++));
			imwa.setWeekAimID(rs.getInt(i++));
			imwa.setTPID(rs.getString(i++));
			imwa.setCorePurpose(rs.getString(i++));
			
			al.add(imwa);
		}
		
		rs.close();
		stmt.close();
		conn.close();
		
		return al;
	}
	
	public ArrayList<String> getTPIDSSByIWeekArrangeID(String iWeekArrangeID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("select TPID from individualmonthweekaim where IWeekArrangeID = '" + iWeekArrangeID + "' and TPID<>'' ");
		
		rs = stmt.executeQuery(sqlStr.toString());
		
		ArrayList<String> al = new ArrayList<String>();
	
		while (rs.next()) {
			al.add(rs.getString(1));
		}
		rs.close();
		stmt.close();
		conn.close();
		
		ArrayList<String> tpids = new ArrayList<String>();
		for(String s:al){
			if(s.indexOf(",")==-1){
				tpids.add(s);
			}else{
				for(String temp : s.split(",")){
					tpids.add(temp);
				}
			}
		}
		
		return tpids;
	}
	
	public List<TopicTransferModel> queryTopicModels(List<String> topicIDs) throws SQLException{
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		List<TopicTransferModel> result = new ArrayList<TopicTransferModel>();
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append("select tpbi.ActivityName, itp.CorePurpose from individualteachingplan itp left join teachingplanbaseinfo tpbi on tpbi.TPID=itp.TPID where 1<>1");
		
		for(String s : topicIDs){
			sqlStr.append(" or tpbi.TPID='").append(s).append("'");
		}
		
		rs = stmt.executeQuery(sqlStr.toString());
		
	
		while (rs.next()) {
			result.add(new TopicTransferModel(rs.getString(1),rs.getInt(2)));
		}
		rs.close();
		stmt.close();
		conn.close();
		
		return result;
	}
}
